package com.hackathon.sgstern.overview;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import android.app.Dialog;
import android.content.Intent;
import android.content.IntentSender;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.hackathon.sgstern.Overview;
import com.hackathon.sgstern.R;
import com.hackathon.sgstern.SurroundingArea;
import com.hackathon.sgstern.Search.ErrorDialogFragment;
import com.hackathon.sgstern.dataprovider.DataProvider;
import com.hackathon.sgstern.joinevent.JoinEvent;
import com.hackathon.sgstern.location.LocationUtils;
import com.hackathon.sgstern.model.SportEvent;
import com.hackathon.sgstern.overview.SportEventListAdapter;

/**
 * A dummy fragment representing a section of the app, but that simply displays
 * dummy text.
 */
public class OverviewNearEventsSectionFragment extends Fragment {

	private static DataProvider dataProvider;
	private static List<SportEvent> events = new ArrayList<SportEvent>();
	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	public static final String ARG_SECTION_NUMBER = "section_number";

	SportEventListAdapter adapter;

	public OverviewNearEventsSectionFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_overview_dummy,
				container, false);

		ListView myEventsList = (ListView) rootView
				.findViewById(R.id.myEventlist);

		adapter = new SportEventListAdapter(getActivity(),
				new ArrayList<SportEvent>());
		myEventsList.setAdapter(adapter);

		myEventsList
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent,
							final View view, int position, long id) {
						final SportEvent clickedEvent = (SportEvent) parent
								.getItemAtPosition(position);
						Intent joinEvent = new Intent(getActivity(),
								JoinEvent.class);
						joinEvent.putExtra(SurroundingArea.JOIN_EVENT_KEY,
								clickedEvent);
						startActivity(joinEvent);
					}

				});

		return rootView;
	}

	public void loadLocationBasedData(Overview overview) {
		dataProvider = DataProvider.getInstance();

		Location currentLoc = overview.getLocation();
		TreeMap<Float, SportEvent> events = new TreeMap<Float, SportEvent>();
		events.clear();
		for (SportEvent event : dataProvider.getEvents()) {
			Location loc = new Location(event.title);
			loc.setLatitude(event.getOrt().latitude);
			loc.setLongitude(event.getOrt().longitude);
			events.put(Float.valueOf(currentLoc.distanceTo(loc)), event);
		}
		if (adapter != null) {
			adapter.sportEvents = new ArrayList<SportEvent>(events.values());
			adapter.distances = new ArrayList<Float>(events.keySet());
			adapter.notifyDataSetChanged();
		}

	}

}
